//
//  ChildInputCell.m
//  yixintong8127
//
//  Created by longzhiyuan on 15/5/5.
//
//
#import "ChildInputCell.h"
#import "MBProgressHUD.h"
#import "GlobalVar.h"
#import "ASIFormDataRequest.h"
#import "iToast.h"
//
@implementation ChildInputCell

- (void)awakeFromNib
{
    // Initialization code
}
- (IBAction)btnOk_click:(UIButton *)sender
{
    
    if (self.txtCard.isFirstResponder)
    {
        [self.txtCard resignFirstResponder];
    }
    if (self.txtPassword.isFirstResponder)
    {
        [self.txtPassword resignFirstResponder];
    }
    if ([self.txtCard.text isEqualToString:@""])
    {
        iToast* toast=[iToast makeText:@"请输入卡号"];
        [toast show];
        return;
    }
    if ([self.txtPassword.text isEqualToString:@""])
    {
        iToast* toast=[iToast makeText:@"请输入密码"];
        [toast show];
        return;
    }
    NSString *nsStrPhone = [GlobalVar loginUserKey];
    //type 类型，比如：中国移动、中国电信、中国联通，分别是：0,1,2
    NSString *nsStrTypeName;
    switch ([GlobalVar getTypeByPhoneNumber])
    {
        case 0:
            nsStrTypeName = @"中国移动";
            break;
        case 1:
            nsStrTypeName = @"中国电信";
            break;
        case 2:
            nsStrTypeName = @"中国联通";
            break;
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示：" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    if([self.nsStrType isEqualToString:@"流量充值"])
    {
        alertView.message = [NSString stringWithFormat:@"%@\n%@\n%@",nsStrTypeName,nsStrPhone,@"\n(首次充值的号码，请先致电运营商，咨询能否使用流量叠加包)"];
    }else{
        alertView.message = [NSString stringWithFormat:@"%@\n%@",nsStrTypeName,nsStrPhone];
    }
    [alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self chongZhi];
    }
}
-(void)chongZhi
{
    if(self.delegate)
    {
        [self.delegate childInputCellQequest:@"正在充值,请稍候..."];
    }
    /*
    HUD = [[[MBProgressHUD alloc] initWithView:self]autorelease];
    HUD.removeFromSuperViewOnHide = YES;
    [self addSubview:HUD];
    [HUD setLabelText:@"正在充值,请稍候..."];
    [HUD show:YES];
     */
    //
    if ([self.nsStrType isEqualToString:@"品牌充值卡"])
    {
        [self requestChongZhiHandle:@"3"];
    }else if ([self.nsStrType isEqualToString:@"移动充值卡"])
    {
        [self requestChongZhiHandle:@"1"];
    }else if ([self.nsStrType isEqualToString:@"联通充值卡"])
    {
        [self requestChongZhiHandle:@"2"];
    }else if ([self.nsStrType isEqualToString:@"电信充值卡"])
    {
        [self requestChongZhiHandle:@"4"];
    }else if([self.nsStrType isEqualToString:@"流量充值"])
    {
        NSString *nsStrCard = self.txtCard.text;
        NSString *nsStrPassword = self.txtPassword.text;
        NSString* _priMD5=[NSString stringWithFormat:@"%@%@%@%@",[GlobalVar loginUserKey],nsStrCard,nsStrPassword,@"ysw"];
        NSString* MD5Str=[GlobalVar md5Str:_priMD5];
        requestChongZhi=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://61.146.138.146:8008/lyb/tel/cardpay.php"]];
        [requestChongZhi setDelegate:self];
        [requestChongZhi setTimeOutSeconds:30.0f];
        [requestChongZhi setPostValue:[GlobalVar loginUserKey] forKey:@"mobile"];
        [requestChongZhi setPostValue:nsStrCard forKey:@"card"];
        [requestChongZhi setPostValue:nsStrPassword forKey:@"pass"];
        [requestChongZhi setPostValue:MD5Str forKey:@"md5"];
        [requestChongZhi setDidFinishSelector:@selector(liuliangchongzhiSuccess:)];
        [requestChongZhi setDidFailSelector:@selector(requestFail:)];
        [requestChongZhi startAsynchronous];
        [self startTimer];
    }
}

-(void)requestChongZhiHandle:(NSString *)type
{
    NSString *nsStrCard = self.txtCard.text;
    NSString *nsStrPassword = self.txtPassword.text;
    NSString *Number = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_NUMBER"]];
    NSString* _priMD5=[NSString stringWithFormat:@"%@%@%@%@",Number,SOFT_ID,nsStrPassword,@"ysw"];
    NSString* MD5Str=[GlobalVar md5Str:_priMD5];
    //NSString* certNum=[[NSUserDefaults standardUserDefaults] objectForKey:@"CERT_NUM"];
    //NSLog(@"==blnUseProxy===%d",[GlobalVar sharedInstance].blnUseProxy);
    if ([GlobalVar sharedInstance].blnUseProxy==YES)
    {
        requestChongZhi=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",origin_url_prefix,@"pay.php"]]];
        [requestChongZhi setProxyPort:ProxyPort];
        [requestChongZhi setProxyHost:[GlobalVar getTheBestIp]];
        //NSLog(@"=====代理＝＝＝＝充值");
        
    }else{
        [requestChongZhi setPostValue:origin_url_prefix forKey:@"apiurl"];
        [requestChongZhi setPostValue:@"pay" forKey:@"act"];
    }
    [requestChongZhi setDelegate:self];
    [requestChongZhi setTimeOutSeconds:30.0f];
    [requestChongZhi setPostValue:Number forKey:@"account"];
    [requestChongZhi setPostValue:nsStrPassword forKey:@"password"];
    [requestChongZhi setPostValue:nsStrCard forKey:@"pin"];
    [requestChongZhi setPostValue:@"" forKey:@"cert"];
    [requestChongZhi setPostValue:SOFT_ID forKey:@"softid"];
    [requestChongZhi setPostValue:@"iphone" forKey:@"platform"];
    //[request setPostValue:@"3" forKey:@"type"];
    [requestChongZhi setPostValue:type forKey:@"type"];
    [requestChongZhi setPostValue:@"10" forKey:@"money"];
    [requestChongZhi setPostValue:MD5Str forKey:@"md5"];
    [requestChongZhi setPostValue:MULTIAGENTSWITCH forKey:@"multiAgent"];
    [requestChongZhi setPostValue:NETWORKSTATE forKey:@"apn"];
    [requestChongZhi setPostValue:SOFT_VER forKey:@"version"];
    [requestChongZhi setDidFinishSelector:@selector(chongzhiSuccess:)];
    [requestChongZhi setDidFailSelector:@selector(requestFail:)];
    [requestChongZhi startAsynchronous];
    [self startTimer];

}
-(void)startTimer
{
    //NSLog(@"=====start=======");
    if (!self.timer)
    {
        self.timer = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(downCount) userInfo:nil repeats:YES];
    }
    //必须手动加入到当前循环中去
    NSRunLoop *runloop=[NSRunLoop currentRunLoop];
    [runloop addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
-(void)stopTimer
{
    if (self.timer)
    {
        [self.timer invalidate];
        self.timer =nil;
    }
}
-(void)downCount
{
    NSLog(@"====downCount===");
    [self stopTimer];
    if (self.delegate)
    {
        [self.delegate childInputCellQequestSuccess];
    }
    //[HUD hide:YES];
    if (requestChongZhi)
    {
        [requestChongZhi cancel];
    }
    iToast* toast=[iToast makeText:@"网络不畅,连接超时!"];
    [toast show];
}
//自家品牌充值回调
-(void)chongzhiSuccess:(ASIHTTPRequest*)request
{
    [self stopTimer];
    if (self.delegate)
    {
        [self.delegate childInputCellQequestSuccess];
    }
    //[HUD hide:YES];
    NSString* result=[request responseString];
    if ([result length]>0)
    {
        NSLog(@"自家品牌充值回调结果===:%@",result);
        NSArray* comArr=[result componentsSeparatedByString:@"|"];
        if ([comArr count]>0)
        {
            
            NSString* code=[comArr objectAtIndex:0];
            if ([code isEqualToString:@"1"])
            {
                
                iToast* toast=[iToast makeText:@"充值成功"];
                [toast show];
            }else
            {
                NSString* errorMsg=[comArr objectAtIndex:1];
                iToast* toast=[iToast makeText:errorMsg];
                [toast show];
            }
            
        }
        
    }
    
}
#pragma mark流量充值回调
-(void)liuliangchongzhiSuccess:(ASIHTTPRequest*)request
{
    [self stopTimer];
    if (self.delegate)
    {
        [self.delegate childInputCellQequestSuccess];
    }
    //[HUD hide:YES];
    NSString* feedbackString=[request responseString];
    //NSLog(@"======liuliangchongzhiSuccess======%@",feedbackString);
    if ([feedbackString length]>0)
    {
        NSArray* feedbackArr=[feedbackString componentsSeparatedByString:@"|"];
        if (feedbackArr.count==2)
        {
            self.txtPassword.text = @"";
            self.txtCard.text = @"";
            iToast* toast=[iToast makeText:[feedbackArr objectAtIndex:1]];
            [toast show];
        }
    }
}
-(void)requestFail:(ASIHTTPRequest*)request
{
    NSLog(@"======requestFail======");
    [self stopTimer];
    if (self.delegate)
    {
        [self.delegate childInputCellQequestFail];
    }
    //[HUD hide:YES];
    iToast* toast=[iToast makeText:ERRORCODE];
    [toast show];
}


-(void)setDataType:(NSString *)type
{
    self.nsStrType = type;
    NSLog(@"ChildInputCell==setDataType===%@",self.nsStrType);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc
{
    [_txtCard release];
    [_txtPassword release];
    [_labFootInfo release];
    [super dealloc];
}
@end
